//
//  AppendXGFishpondViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/7/22.
//

#import "AppendXGFishpondViewController.h"
#import "AppendXGFishpondHeaderView.h"
#import "AppendXGFishpondFooterView.h"
#import "AppendFishpondTableViewCell.h"
#import "AddPondModel.h"
#import "YTSelectStringShowView.h"
#import "getPondTissueListModel.h"
#import "DTChoosePersonListShowView.h"
#import "SetupXGWorkViewController.h"
#import "YTTipsShowView.h"
#import "AppendXGFishpondTableViewCell.h"
#import "AppendSettingShowView.h"

#import "PersonInCchargeShowView.h"
#import "foundQuestSelectPondShowView.h"
@interface AppendXGFishpondViewController ()
@property (nonatomic ,strong) UIButton * tjBtn;
@property (nonatomic ,strong) AddXGPondModel * model;
@property (nonatomic ,strong) NSArray <pondTypeModel *>* TypemodelArray;

@end

@implementation AppendXGFishpondViewController
- (NSArray<pondTypeModel *> *)TypemodelArray{
    if(!_TypemodelArray){
        _TypemodelArray = [NSArray array];
    }
    return _TypemodelArray;
}
- (AddXGPondModel *)model{
    if(!_model){
        _model = [AddXGPondModel new];
    }
    return _model;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.shutDownBack = YES;
    self.NoDataHidden = YES;
    [self setupView];
    
    [self PondDetailsApi];
    
}

- (void)PondDetailsApi{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.ID forKey:@"id"];
    [SCNetManger PondManagePondManageDetailOneWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            AddXGPondModel * xqmodel = [AddXGPondModel yy_modelWithJSON:model.result];
            xqmodel.isAll = self.isAll;
            xqmodel.batchFlag = self.isAll?1:0;
            xqmodel.isGroup = self.isGroup;
            xqmodel.TJpictureImgStr = xqmodel.pictureImg;
            xqmodel.pictureImg = xqmodel.pictureImgUrl;
            self.model = xqmodel;
            [self.tableview reloadData];
            [self loadNewData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

///池塘类型.默认选第一个
- (void)loadNewData {
    [self.view showBusyHUD];
    [SCNetManger PostPondTypeGetOptionWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <pondTypeModel *>* array = [NSArray yy_modelArrayWithClass:pondTypeModel.class json:model.result ];
            self.TypemodelArray = array;
            for (pondTypeModel * pondModel in array) {
                if([pondModel.ID isEqualToString:self.model.typeId]){
                    self.model.typeName = pondModel.name;
                    return;
                }
            }
        } else {
            [self.view showWarning:model.message];
        }
    }];
}

- (void)setupView{
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        if(weakSelf.model.isNext){
            weakSelf.model.isNext = NO;
            [weakSelf.tableview reloadData];
            return;
        }
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(140));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"设置池塘" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    
    UIView * buttomview = [UIView new];
    buttomview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:buttomview];
    [buttomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63)+ KSafeAreaBottomHeight);
    }];
    
    UIButton * tjBtn = [UIButton buttonWithType:0];
    self.tjBtn = tjBtn;
    tjBtn.addNormalText(@"下一步").addNormalColor(UIColor.whiteColor).addFont(kCFont(18)).addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(355), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]).addTarget(self,@selector(CMApi));
    [DTKTool YJ:kwt(10) andView:tjBtn];
    [buttomview addSubview:tjBtn];
    [tjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.height.offset(kwt(48));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(7.5));
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(topview.mas_bottom);
        make.bottom.equalTo(buttomview.mas_top);
    }];
    
}

-(void)BCBtnClick{
    ///保存
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)lastObject];
    NSString *accountPath = [documentPath stringByAppendingPathComponent:@"AppendXGFishpondViewController"];
    [NSKeyedArchiver archiveRootObject:self.model toFile:accountPath];
    [self.view showWarning:@"保存成功"];
}
- (void)CMApi{
    
    if(self.model.tissueId.length < 1){
        [self.view showWarning:@"请选择池塘编组"];
        return;
    }
    
    NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:@{
        @"orgCode":[UserInfoModel shared].OrgCode,
        @"pondId":self.ID,
    }];
    NSMutableArray * nameAndTissueIdList = [NSMutableArray array];
//    if(self.model.isAll && self.model.isGroup){
//        for (addPondModelListModel * model in self.model.addPondModelList) {
//            if(model.name.length < 1){
//                [self.view showWarning:@"请输入池塘名称"];
//                return;
//            }
//            if(model.tissueId.length < 1){
//                [self.view showWarning:@"请输入池塘编组"];
//                return;
//            }
//            NSMutableDictionary * AndTissueIdList = [NSMutableDictionary dictionary];
//            [AndTissueIdList setObject:model.name forKey:@"name"];
//            [AndTissueIdList setObject:model.tissueId forKey:@"tissueId"];
//            [nameAndTissueIdList addObject:AndTissueIdList];
//        }
//    }
    
    NSMutableDictionary * AndTissueIdList = [NSMutableDictionary dictionary];
    [AndTissueIdList setObject:self.model.name forKey:@"name"];
    [AndTissueIdList setObject:self.model.tissueId forKey:@"tissueId"];
    [nameAndTissueIdList addObject:AndTissueIdList];
    
    [dict setObject:nameAndTissueIdList forKey:@"nameAndTissueIdList"];
    [self.view showBusyHUD];
    
    [SCNetManger PostPondManageIsDuplicateNameWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSNumber *boolNumber = [model.result objectForKey:@"isOk"]; // 或者使用 valueForKey:@"isOn"
            if([boolNumber boolValue]){
                [self TJBtnClick];
            } else {
                [self.view showWarning:@"池塘名称重复,请重新取名"];
            }
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)TJBtnClick{
    
    if(self.model.typeId.length < 1){
        [self.view showWarning:@"请选择池塘类型"];
        return;
    }
    if(self.model.name.length < 1){
        [self.view showWarning:@"请输入池塘名称"];
        return;
    }
    if(self.model.isAll){///多池塘
        if(self.model.isNext){
            if(self.model.typeCode == 0){///衍生少1个
                
            } else {
                if(self.model.breedTypeId.length < 1){
                    [self.view showWarning:@"请选择养殖种类"];
                    return;
                }
                if(self.model.breedCycle.intValue < 1){
                    [self.view showWarning:@"请输入养殖周期"];
                    return;
                }
            }
            if(self.model.directorName.length < 1){
                [self.view showWarning:@"请选择负责人"];
                return;
            }
            if(self.model.area.floatValue == 0){
                [self.view showWarning:@"请输入池塘面积"];
                return;
            }
            if(self.model.pictureImgArray.count < 1){
                [self.view showWarning:@"请上传池塘照片"];
                return;
            }
            if(self.model.templateId.length < 1){
                [self.view showWarning:@"请选择工作模版"];
                return;
            }
        
        } else {
            if(self.model.isAll) {
                //去下一步
                self.model.isNext = YES;
                [self.tableview reloadData];
                return;
            }
        }
    } else {///单池塘设置
        if(self.model.typeCode == 0){///衍生少1个
            
        } else {
            if(self.model.breedTypeId.length < 1){
                [self.view showWarning:@"请选择养殖种类"];
                return;
            }
            if(self.model.breedCycle.intValue < 1){
                [self.view showWarning:@"请输入养殖周期"];
                return;
            }
        }
        
        
        if(self.model.directorName.length < 1){
            [self.view showWarning:@"请选择负责人"];
            return;
        }
        if(self.model.area.intValue < 1){
            [self.view showWarning:@"请输入池塘面积"];
            return;
        }
        if(self.model.pictureImgArray.count < 1){
            [self.view showWarning:@"请上传池塘照片"];
            return;
        }
        if(self.model.templateId.length < 1){
            [self.view showWarning:@"请选择工作模版"];
            return;
        }
    }
    
    [self openwork];
}

- (void)openwork{
    if(self.model.ifReselectTemplate == 0){
        SetupXGWorkViewController * vc = [SetupXGWorkViewController new];
        vc.model = self.model;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:@{@"id":self.model.templateId}];
    
    if(self.model.templateType.intValue == 1){    ///公共模版
        [SCNetManger PostMBDetailWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
            [self.view hideBusyHUD];
            if(model.code == 200){
                TemplateFoundDetailsModel * m = [TemplateFoundDetailsModel yy_modelWithJSON:model.result];
                [m seledModel];
                for (TemplateFoundDetailsStepModelListModel * stempmodel in m.stepModelList) {///要按照新建的模版来处理
                    stempmodel.templateStepId = @"";
                    for (TemplateFoundDetailsTaskTypeModelListModel * taskmodel in stempmodel.taskTypeModelList) {
                        taskmodel.sourceFlag = 1;
                    }
                }
                SetupXGWorkViewController * vc = [SetupXGWorkViewController new];
                self.model.stepModelList = [NSMutableArray arrayWithArray: m.stepModelList];
                self.model.templateVersionNumber = m.version;
                vc.model = self.model;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                [self.view showWarning:model.message];
            }
        }];
        
    }else  if(self.model.templateType.intValue == 2) {
        [SCNetManger PostGRMBDetailWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
            [self.view hideBusyHUD];
            if(model.code == 200){
                TemplateFoundDetailsModel * m = [TemplateFoundDetailsModel yy_modelWithJSON:model.result];
                [m seledModel];
                for (TemplateFoundDetailsStepModelListModel * stempmodel in m.stepModelList) {///要按照新建的模版来处理
                    stempmodel.templateStepId = @"";
//                    for (TemplateFoundDetailsTaskTypeModelListModel * taskmodel in stempmodel.taskTypeModelList) {
//                        taskmodel.sourceFlag = 1;
//                    }
                }
                SetupXGWorkViewController * vc = [SetupXGWorkViewController new];
                self.model.stepModelList = [NSMutableArray arrayWithArray: m.stepModelList];
                self.model.templateVersionNumber = m.version;
                vc.model = self.model;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                [self.view showWarning:model.message];
            }
        }];
    }
    
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    AppendXGFishpondHeaderView * headerview = [AppendXGFishpondHeaderView NewView];
    headerview.model = self.model;
    MJWeakSelf
    headerview.openTKLXBlock = ^(AddXGPondModel * _Nonnull model) {
        [weakSelf openTKLX:model];
    };
    headerview.openTKBZBlock = ^(AddXGPondModel * _Nonnull model) {
        [weakSelf openTKBZ:model];
    };
    headerview.openSZFSBlock = ^(AddXGPondModel * _Nonnull model) {
        [weakSelf openSZFS:model];
    };
    return headerview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.model.XGHeaderViewH;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    AppendXGFishpondFooterView * footerview = [AppendXGFishpondFooterView NewView];
    footerview.model = self.model;
    MJWeakSelf
    //选择负责人
    footerview.openFZRBlock = ^(AddXGPondModel * _Nonnull model) {
        [weakSelf openFZR:model];
    };
    ///选择工作模版
    footerview.openGZMBBlock = ^(AddXGPondModel * _Nonnull model) {
        [weakSelf openGZMB:model];
    };
    ///选择批量打卡
    footerview.openPLDKBlock = ^(AddPondModel * _Nonnull model) {
        [weakSelf openPLDK:model];
    };
    ///选择工作时间
    footerview.openGZSJBlock = ^(AddXGPondModel * _Nonnull model) {
        [weakSelf openGZSJ:model];
    };
    
    ///打开照片
    footerview.openImageBlock = ^(AddXGPondModel * _Nonnull model, int index) {
        [weakSelf openImgArray:model.pictureImgArray andindex:index];
    };
    ///删除照片
    footerview.deleteImageBlock = ^(AddXGPondModel * _Nonnull model, int index) {
        [weakSelf.model.pictureImgArray removeObjectAtIndex:index];
        if(weakSelf.model.TJpictureImgArray.count > index){
            [weakSelf.model.TJpictureImgArray removeObjectAtIndex:index];
        }
        [weakSelf.tableview reloadData];
    };
    ///添加照片
    footerview.openAddImageBlock = ^(AddXGPondModel * _Nonnull model) {
        [weakSelf openXJWithType:ACMediaPickerSourceFromAll andBlock:^(NSArray<ACMediaModel *> * _Nonnull list) {
            [SCNetManger PostuploadFilesWithImg:list.firstObject.image CompletionHandler:^(NSString * _Nonnull url, NSString * _Nonnull previewUrl, UIImage * _Nonnull img) {
                [weakSelf.model.pictureImgArray addObject:previewUrl];
                [weakSelf.model.TJpictureImgArray addObject:url];
                [weakSelf.tableview reloadData];
            }];
        }];
    };
    ///养殖种类
    footerview.openYZZLBlock = ^(AddPondModel * _Nonnull model) {
        [weakSelf openYZZL:model];
    };
    return footerview;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.model.XGFooterViewH;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.model.isAll && self.model.isNext == NO){
        return 1;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    if(self.model.isAll && self.model.isGroup && self.model.isNext == NO){
    //        return kwt(400);
    //    }
    return self.model.XGCellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    MJWeakSelf
    if(self.model.isAll && self.model.isGroup && self.model.isNext == NO){
        
        AppendXGFishpondTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AppendXGFishpondTableViewCell"];
        if (cell == nil) {
            cell = [[AppendXGFishpondTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppendXGFishpondTableViewCell"];
        }
        cell.model = self.model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell ;
    }
    AppendFishpondTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AppendFishpondTableViewCell"];
    if (cell == nil) {
        cell = [[AppendFishpondTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppendFishpondTableViewCell"];
    }
    cell.model = self.model;
    cell.Index = indexPath.row + 1;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
}

- (void)openSZFS:(AddXGPondModel *)model{
    MJWeakSelf
    if(model.isGroup == NO){
        [self.view showWarning:@"只能设置当前池塘"];
        return;
    }
    [AppendSettingShowView showViewWithType:(self.model.isAll?2:1) oneBlock:^{
       
        if(weakSelf.model.isAll == YES){
            weakSelf.model.isAll = NO;
            weakSelf.model.batchFlag = weakSelf.model.isAll?1:0;
            weakSelf.model.templateId = @"";
            weakSelf.model.templateType = @"";
            weakSelf.model.templateName = @"";
            weakSelf.model.typeId = @"";
            weakSelf.model.typeName = @"";
            weakSelf.model.typeCode = 0;
        }
        [weakSelf.tableview reloadData];
    } andallBlock:^{
        if(weakSelf.model.isAll == NO){
            weakSelf.model.isAll = YES;
            weakSelf.model.batchFlag = weakSelf.model.isAll?1:0;
            weakSelf.model.templateId = @"";
            weakSelf.model.templateType = @"";
            weakSelf.model.templateName = @"";
            weakSelf.model.typeId = @"";
            weakSelf.model.typeName = @"";
            weakSelf.model.typeCode = 0;
        }
        [weakSelf.tableview reloadData];

    }];
    
}
///点击池塘类型
- (void)openTKLX:(AddXGPondModel *)model{
    int i = 0;
    int index = 0;
    NSMutableArray * array = [NSMutableArray array];
    for (pondTypeModel * typeModel in self.TypemodelArray) {
        if(model.batchFlag == 1){///全部只能选养殖
            if(typeModel.typeCode.intValue == 1){///养殖类
                if([typeModel.ID isEqualToString:model.typeId]){
                    index = i;
                }
                [array addObject:typeModel.name];
                i ++;
            }
        } else {///单个不限制
            if([typeModel.ID isEqualToString:model.typeId]){
                index = i;
            }
            [array addObject:typeModel.name];
            i ++;
        }
    }
    
  
    MJWeakSelf
    [YTSelectStringShowView showViewWithTitle:@"选择池塘类型" andIndex:index andDateArray:array andBlock:^(NSString * _Nonnull str) {
        if(![weakSelf.model.typeName isEqualToString:str]){
            ///产量
//            weakSelf.model.expectYield = @"";
            ///养殖周期
//            weakSelf.model.breedCycle = @"";
            ///养殖种类
//            weakSelf.model.breedTypeId = @"";
//            weakSelf.model.breedTypeName = @"";
            weakSelf.model.templateId = @"";
            weakSelf.model.templateType = @"";
            weakSelf.model.templateName = @"";
        }
        for (pondTypeModel * typeModel in weakSelf.TypemodelArray) {
            if([str isEqualToString:typeModel.name]){
                weakSelf.model.typeId = typeModel.ID;
                weakSelf.model.typeName = typeModel.name;
                weakSelf.model.typeCode = [typeModel.typeName isEqualToString:@"衍生类"]?0:1;
                [weakSelf.tableview reloadData];
                return;
            }
        }
    }];
}
///池塘编组
- (void)openTKBZ:(AddXGPondModel *)model{
    [self.view showBusyHUD];
    [SCNetManger PostPondTissueGetPondTissueListWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <getPondTissueListModel *>* array = [NSArray yy_modelArrayWithClass:getPondTissueListModel.class json:model.result ];
            getPondTissueListModel * mm = [getPondTissueListModel new];
            mm.children = array;
            if(self.model.tissueId.length > 0){
                [mm modelWithSeledID:self.model.tissueId];
            }
            NSLog(@"%@",array);
            MJWeakSelf
            [DTChoosePersonListShowView showViewWithArray:array andBlock:^(getPondTissueListModel * _Nonnull pondModel) {
                weakSelf.model.tissueId = pondModel.ID;
                weakSelf.model.tissueName = pondModel.TJname;
                
                [weakSelf.tableview reloadData];
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
///养殖种类
- (void)openYZZL:(AddXGPondModel *)Model{
    [self.view showBusyHUD];
    [SCNetManger PostBreedTypeGetOptionWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <getPondTissueListModel *>* modelarray = [NSArray yy_modelArrayWithClass:getPondTissueListModel.class json:model.result ];
//            int i = 0;
//            int index = 0;
//            NSMutableArray * array = [NSMutableArray array];
//            for (getPondTissueListModel * typeModel in modelarray) {
//                if([typeModel.ID isEqualToString:Model.breedTypeId]){
//                    index = i;
//                }
//                [array addObject:typeModel.name];
//                i ++;
//            }
//            MJWeakSelf
//            [YTSelectStringShowView showViewWithTitle:@"选择养殖种类" andIndex:index andDateArray:array andBlock:^(NSString * _Nonnull str) {
//                for (getPondTissueListModel * typeModel in modelarray) {
//                    if([str isEqualToString:typeModel.name]){
//                        weakSelf.model.breedTypeId = typeModel.ID;
//                        weakSelf.model.breedTypeName = typeModel.name;
//                        [weakSelf.tableview reloadData];
//                        return;
//                    }
//                }
//            }];
            for (NSString * ID in [Model.breedTypeId componentsSeparatedByString:@","]) {
                for (getPondTissueListModel * typeModel in modelarray) {
                    if([typeModel.ID isEqualToString:ID]){
                        typeModel.seled = YES;
                    }
                }
            }
            MJWeakSelf
            
            [foundYZZLSelectPondShowView showviewWithArray:modelarray andBlock:^(NSArray<NSString *> * _Nonnull array) {
                NSMutableArray * nameArray = [NSMutableArray array];
                NSMutableArray * idArray = [NSMutableArray array];
                
                for (getPondTissueListModel * typeModel in modelarray) {
                    for (NSString * ID in array) {
                        if([ID isEqualToString:typeModel.ID]){
                            [nameArray addObject:typeModel.name];
                            [idArray addObject:typeModel.ID];
                        }
                    }
                }
                weakSelf.model.breedTypeId = [idArray componentsJoinedByString:@","];
                weakSelf.model.breedTypeName = [nameArray componentsJoinedByString:@","];
                [weakSelf.tableview reloadData];
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
//选择负责人
- (void)openFZR:(AddXGPondModel *)Model{
    [self.view showBusyHUD];
    [SCNetManger PostGetPositionAndUserByOrgCodeWithDict: [NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <PositionUserModel *>* modelarray = [NSArray yy_modelArrayWithClass:PositionUserModel.class json:model.result];
            MJWeakSelf
            for (PositionUserModel * typeModel in modelarray) {
                for (PondUserModel * usermodel in typeModel.userModelList) {
                    if([Model.directorName isEqualToString:usermodel.realName]){
                        usermodel.isSeled = YES;
                    }
                }
            }
            [SearchPersonInCchargeShowView showviewWithTitle:@"负责人" andArray:modelarray andBlock:^(NSString * _Nonnull ID) {
                for (PositionUserModel * typeModel in modelarray) {
                    for (PondUserModel * usermodel in typeModel.userModelList) {
                        if([ID isEqualToString:usermodel.userId]){
                            weakSelf.model.directorPhone = usermodel.phone;
                            weakSelf.model.directorName = usermodel.realName;
                            [weakSelf.tableview reloadData];
                            return;
                        }
                    }
                }
            }];
//            NSMutableArray <PondUserModel *>* newModelArray = [NSMutableArray array];
//            for (PositionUserModel * typeModel in modelarray) {
//                for (PondUserModel * usermodel in typeModel.userModelList) {
//                    BOOL isHave = NO;
//                    for (PondUserModel * newModel in newModelArray) {
//                        if([newModel.userId isEqualToString:usermodel.userId]){
//                            isHave = YES;
//                        }
//                    }
//                    if(isHave == NO){
//                        [newModelArray addObject:usermodel];
//                    }
//                }
//            }
//            
//            MJWeakSelf
//            int i = 0;
//            int index = 0;
//            NSMutableArray * array = [NSMutableArray array];
//            for (PondUserModel * typeModel in newModelArray) {
//                if([Model.directorName isEqualToString:typeModel.realName]){
//                    index = i;
//                }
//                [array addObject:typeModel.realName];
//                i ++;
//            }
//            
//            [YTSelectStringShowView showViewWithTitle:@"选择负责人" andIndex:index andDateArray:array andBlock:^(NSString * _Nonnull str) {
//                for (PondUserModel * typeModel in newModelArray) {
//                    if([str isEqualToString:typeModel.realName]){
//                        weakSelf.model.directorPhone = typeModel.phone;
//                        weakSelf.model.directorName = typeModel.realName;
//                        [weakSelf.tableview reloadData];
//                        return;
//                    }
//                }
//            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}
///工作模版
- (void)openGZMB:(AddXGPondModel *)Model{
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setObject:self.model.typeId forKey:@"pondId"];
    [SCNetManger PostTemplateOptionListWithDict:dict  andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code ==200){
            NSArray <templateOptionListModel *>* modelarray = [NSArray yy_modelArrayWithClass:templateOptionListModel.class json:model.result ];
            MJWeakSelf
            int i = 0;
            int index = 0;
            NSMutableArray * array = [NSMutableArray array];
            for (templateOptionListModel * typeModel in modelarray) {
                if([typeModel.ID isEqualToString:Model.templateId]){
                    index = i;
                }
                [array addObject:typeModel.name];
                i ++;
            }
            
            [YTSelectStringShowView showViewWithTitle:@"选择工作模版" andIndex:index andDateArray:array andBlock:^(NSString * _Nonnull str) {
                for (templateOptionListModel * typeModel in modelarray) {
                    if([str isEqualToString:typeModel.name]){
                        if(Model.templateId.length < 1){///新增
                            weakSelf.model.templateId = typeModel.ID;
                            weakSelf.model.templateType = typeModel.templateType;
                            weakSelf.model.templateName = typeModel.name;
                            weakSelf.model.ifReselectTemplate = 1;
                            [weakSelf.tableview reloadData];
                        } else {
                            [YTTipsShowView showviewWithTips:@"是否变更工作内容" andRightBlock:^{
                                weakSelf.model.templateId = typeModel.ID;
                                weakSelf.model.templateType = typeModel.templateType;
                                weakSelf.model.templateName = typeModel.name;
                                weakSelf.model.ifReselectTemplate = 1;
                                [weakSelf.tableview reloadData];
                            }];
                        }
                        
                        return;
                    }
                }
            }];
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
///是否批量
- (void)openPLDK:(AddXGPondModel *)model{
    MJWeakSelf
    [YTSelectStringShowView showViewWithTitle:@"是否批量" andIndex:(model.batchFlag == 1)? 0 : 1 andDateArray:@[@"是",@"否"]  andBlock:^(NSString * _Nonnull str) {
        weakSelf.model.batchFlag = [str isEqualToString:@"是"]?1:0;
        [weakSelf.tableview reloadData];
    }];
}
///执行时间
- (void)openGZSJ:(AddXGPondModel *)model{
    MJWeakSelf
    [BRDatePickerView showDatePickerWithMode:BRDatePickerModeYMD title:@"工作执行日期" selectValue:model.executeDate minDate:[NSDate new] maxDate:nil isAutoSelect:NO resultBlock:^(NSDate * _Nullable selectDate, NSString * _Nullable selectValue) {
        weakSelf.model.executeDate = selectValue;
        [weakSelf.tableview reloadData];
    } resultRangeBlock:nil];
}
@end
